﻿using System;
using System.Collections.Generic;
using System.Linq;
using HiStaff.Base;
using HiStaff.Control;
using HiStaff.Domain.IN;
using HiStaff.Domain.Sys;
using HiStaff.Util;
using HiStaff.Dal;
using Janus.Windows.GridEX;
using System.Data;
using System.Windows.Forms;

namespace HiStaff.iProfile
{
    public partial class Protection : FormBase
    {
        public Protection()
        {
            InitializeComponent();
        }
        protected override bool FormLoad()
        {
            try
            {
                AddBasicCommandBar();
                SetToolStripButtonBar(CommandBarManager.ActionCommand.Cancel);
                BaseStatus = Util.Enum.FormStatus.View;
                SetStatusInputControl(this);
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }
        private void BindData()
        {
            try
            {
                var lstEmployee = OracleHelper.ExcuteSelectMultiObject<IN_ENTITLED>("",
                    new SEARCH
                    {
                        TERMINATE = chkTerminate.Value,
                       // ORGID = trvOrg.OrgId,
                        CHGFROM = hiDatePicker2.Value,
                        CHGTO = hiDatePicker1.Value
                    });

                grdMain.DataSource = lstEmployee;
                grdMain.Refresh();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }
        private void trvOrg_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            try
            {
                BindData();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }
        protected override bool SaveButtonClick()
        {
            try
            {
                var objSave = new IN_ENTITLED();
                ValueControlsToObject(objSave, this);


                if (BaseStatus == Util.Enum.FormStatus.Add)
                {
                    objSave.ID = 0;
                }
                OracleHelper.ExcuteNonQuery("", objSave);
                BindData();
                MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }
        protected override bool CancelButtonClick()
        {
            try
            {
                BindData();
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }
        //chưa sửa store
        protected override bool DeleteButtonClick()
        {
            if (grdMain.SelectedItems.Count > 0)
            {
                var row = grdMain.SelectedItems[0];
                if (row.RowType == Janus.Windows.GridEX.RowType.Record)
                {
                    if (OracleHelper.ExcuteNonQuery("", row.GetRow().DataRow))
                    {
                        MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        BindData();
                        return true;
                    }
                }
            }
            return false;
        }
        private void BindDataFromGrid()
        {
            BindDataFromGridToForm(grdMain, this);
        }

        private void grdMain_SelectionChanged(object sender, EventArgs e)
        {
            BindDataFromGrid();
            //
        }

    }
}
